Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What does the following function do?int fun(u... Start Learning for Free
What does the following function do?
int fun(unsigned int n)
{
if (n == 0 || n == 1)
return n;
 if (n%3 != 0)
return 0;
return fun(n/3);
}
  • a)
    It returns 1 when n is a multiple of 3, otherwise returns 0
  • b)
    It returns 1 when n is a power of 3, otherwise returns 0
  • c)
    t returns 0 when n is a multiple of 3, otherwise returns 1
  • d)
    It returns 0 when n is a power of 3, otherwise returns 1
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
What does the following function do?int fun(unsigned int n){if (n == 0...
Lets solve with example, n = 27 which power of 3. First time if condition is false as n is neither equal to 0 nor equal to 1 then 27%3 = 0. Here, again if condition false because it is equal to 0. Then fun(27/3) will call. Second time if condition is false as n is neither equal to 0 nor equal to 1 then 9%3 = 0. Here again if condition false because it is equal to 0. Then fun (9/3) will call and third time if condition is false as n is neither equal to 0 nor equal to 1 then 3%3 = 0. Here again if condition false because it is equal to 0. Then fun(3/3) will call here n==1 if condition gets true and it return n i.e. 1. Option (B) is correct.
View all questions of this test
Most Upvoted Answer
What does the following function do?int fun(unsigned int n){if (n == 0...
Explanation:

The given function fun() takes an unsigned integer n as input and recursively checks if n is a power of 3 or not. The function works as follows:

1. If n is 0 or 1, then it returns n itself, as both 0 and 1 are powers of 3.
2. If n is not divisible by 3, then it returns 0, as any number that is not divisible by 3 cannot be a power of 3.
3. If n is divisible by 3, then it recursively calls fun() with n/3 as input.

The function keeps dividing n by 3 until it becomes either 0 or 1. At that point, it returns the value of n, which is either 0, 1, or a power of 3.

Example:

Let's say we call the function fun() with input n=81. The function works as follows:

1. n is divisible by 3, so the function calls itself with n/3=27 as input.
2. 27 is divisible by 3, so the function calls itself with 27/3=9 as input.
3. 9 is divisible by 3, so the function calls itself with 9/3=3 as input.
4. 3 is divisible by 3, so the function calls itself with 3/3=1 as input.
5. 1 is not divisible by 3, so the function returns 1.

Since the function returns 1 when n=81, which is a power of 3, option 'B' is the correct answer.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer?
Question Description
What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer?.
Solutions for What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer?, a detailed solution for What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What does the following function do?int fun(unsigned int n){if (n == 0 || n == 1)return n;if (n%3 != 0)return 0;return fun(n/3);}a)It returns 1 when n is a multiple of 3, otherwise returns 0b)It returns 1 when n is a power of 3, otherwise returns 0c)t returns 0 when n is a multiple of 3, otherwise returns 1d)It returns 0 when n is a power of 3, otherwise returns 1Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev